import trigger from "../../effect/trigger";
import { TriggerOpTypes } from "../../utils";

const deleteHandler: ProxyHandler<object>["deleteProperty"] = (target, key) => {
  const hadKey = target.hasOwnProperty(key);

  const result = Reflect.deleteProperty(target, key);

  // 如果有这个key且删除成功了
  if (hadKey && result) {
    // 派发更新
    trigger(target, TriggerOpTypes.DELETE, key);
  }

  return result;
};

export default deleteHandler;
